/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Wink
 */
@Entity
@Table(name = "boards")
@XmlRootElement
@NamedQueries({@NamedQuery(name = "Board.find", query = "SELECT b from Board b"
        + " WHERE b.zoom = :zoom"
        + " AND :south < b.north AND :east > b.west"
        + " AND :north > b.south AND :west < b.east"
        + " AND b.id NOT IN :revealed")})
public class Board implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;
    @NotNull
    @Size(min = 1, max = 40)
    @Column(name = "name")
    private String name;
    @Size(max = 20)
    @Column(name = "user_name")
    private String userName;
    @Column(name = "timestamp")
    @Temporal(TemporalType.TIMESTAMP)
    private Date timestamp;
    @Max(value=90)  @Min(value=-90)
    @Column(name = "north")
    private double north;
    @Max(value=180)  @Min(value=-180)
    @Column(name = "west")
    private double west;
    @Max(value=90)  @Min(value=-90)
    @Column(name = "south")
    private double south;
    @Max(value=180)  @Min(value=-180)
    @Column(name = "east")
    private double east;
    @Column(name = "zoom")
    private int zoom;
    
    @PrePersist 
    protected void onCreate() {
        timestamp = new Date();
    }

    @PreUpdate 
    protected void onUpdate() {
        timestamp = new Date();
    }

    public Board() {
    }

    public Board(String name, double north, double west, 
            double south, double east, int zoom) {
        this.name = name;
        this.north = north;
        this.west = west;
        this.south = south;
        this.east = east;
        this.zoom = zoom;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    public double getNlat() {
        return north;
    }

    public void setNlat(double north) {
        this.north = north;
    }

    public double getWlng() {
        return west;
    }

    public void setWlng(double west) {
        this.west = west;
    }

    public double getSlat() {
        return south;
    }

    public void setSlat(double south) {
        this.south = south;
    }

    public double getElng() {
        return east;
    }

    public void setElng(double east) {
        this.east = east;
    }
    
    public int getZoom() {
    	return zoom;
    }
    
    public void setZoom(int zoom) {
    	this.zoom = zoom;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Board)) {
            return false;
        }
        Board other = (Board) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.Boards[ id=" + id + " ]";
    }
    
}
